<?php
class Category extends Model 
{
	/*Determines whether the given person exists*/
	function exists($category_id)
	{
		$this->db->from('categories');	
		$this->db->where('category_id',$category_id);
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}
	
	/*Gets all people*/
	function get_all()
	{
		$this->db->from('categories');
		$this->db->order_by("category_id", "asc");
		return $this->db->get();		
	}
	
	/*
	Gets information about a person as an array.
	*/
	function get_info($category_id)
	{
		$query = $this->db->get_where('categories', array('category_id' => $category_id), 1);
		
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//create object with empty properties.
			$fields = $this->db->list_fields('categories');
			$category_obj = new stdClass;
			
			foreach ($fields as $field)
			{
				$category_obj->$field='';
			}
			
			return $category_obj;
		}
	}
	
	/*
	Get people with specific ids
	*/
	function get_multiple_info($category_ids)
	{
		$this->db->from('categories');
		$this->db->where_in('category_id',$category_ids);
		$this->db->order_by("category_id", "asc");
		return $this->db->get();		
	}
	
	/*
	Inserts or updates a person
	*/
	function save(&$category_data,$category_id=false)
	{		
		if (!$category_id or !$this->exists($category_id))
		{
			if ($this->db->insert('categories',$category_data))
			{
				$category_data['category_id']=$this->db->insert_id();
				return true;
			}
			
			return false;
		}
		
		$this->db->where('category_id', $category_id);
		return $this->db->update('categories',$category_data);
	}
	
	/*
	Deletes one Person (doesn't actually do anything)
	*/
	function delete($category_id)
	{
		return $this->db->delete('categories', array('category_id' => $category_id));
	}
	
	/*
	Deletes a list of people (doesn't actually do anything)
	*/
	function delete_list($category_ids)
	{	
		$return_flag = true;
		foreach ( $category_ids as $category_id) {
			$return_flag = $return_flag and ($this->db->delete('categories', array('category_id' => $category_id)));
		}
		return $return_flag;
 	}
	
}
?>
